package main.gui.dataobjecteditor;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import data.EditorObject;
import data.ObjectDatabase;

public class IntegerEditorPanel extends DataAttributePanel implements
		ChangeListener {

	private static final long serialVersionUID = 9048268383552320826L;

	private JSpinner intSpinner;
	private int min, max;

	public IntegerEditorPanel(String attrName, String objectGUID,
			String fqClassName, int min, int max) {
		super(attrName, objectGUID, fqClassName);
		this.min = min;
		this.max = max;
	}

	@Override
	public void buildPanel(Object value) {
		SpinnerNumberModel model = new SpinnerNumberModel(
				(int) ((Integer) value), min, max, 1);
		intSpinner = new JSpinner(model);
		intSpinner.setPreferredSize(new Dimension(MIN_WIDGET_WIDTH, MIN_WIDGET_HEIGHT));
		intSpinner.addChangeListener(this);
		this.add(intSpinner, BorderLayout.EAST);
	}

	@Override
	public void stateChanged(ChangeEvent arg0) {
		EditorObject editorObject = ObjectDatabase.getInstance()
				.getEditorObject(fqClassName, objectGUID);
		int value = (Integer) intSpinner.getValue();
		if (editorObject.validateValue(attrName, value)) {
			editorObject.getData().put(attrName, value);
		}
	}

}
